﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Reflection;
using System.Diagnostics;
using System.IO;
using Newtonsoft.Json;

namespace Xinnos.Lca.Yacht.Analyzer.View
{
    internal class ProgramProfileViewModel:INotifyPropertyChanged
    {
        private static ProgramProfileViewModel mInstance = null;
        public event PropertyChangedEventHandler PropertyChanged;
        private string mProduct = null;
        private string mDetailName = null;
        private string mVersion = null;

        public ObservableCollection<MemberViewModel> Members { get; set; }
        public ObservableCollection<ModuleViewModel> Modules { get; set; }

        public string Product
        {
            get { return mProduct; }
            set { mProduct = value; RaisePropertyChanged("Product"); }
        }
        public string DetailName
        {
            get { return mDetailName; }
            set
            {
                mDetailName = value;
                RaisePropertyChanged("DetailName");
            }
        }
        public string Version
        {
            get { return mVersion; }
            set
            {
                mVersion = value;
                RaisePropertyChanged("Version");
            }
        }
        public string Deployment { get { return Properties.Resources.Deployment; } }

        private ProgramProfileViewModel()
        {
            Assembly _executable = Assembly.GetExecutingAssembly();
            FileVersionInfo _version = FileVersionInfo.GetVersionInfo(_executable.Location);
            
            string _postfix = null;
            if (1 > _version.ProductMajorPart) _postfix = "Beta";
            this.Product = _version.ProductName;
            this.DetailName = _version.Comments;
            this.Version = _version.ProductVersion;

            if (!string.IsNullOrEmpty(_postfix))
            {
                string _release = " (" + _postfix + ")";
                this.Product += _release;
                this.DetailName += _release;
            }

            if (null == Members) Members = new ObservableCollection<MemberViewModel>();
            if (null == Modules) Modules = new ObservableCollection<ModuleViewModel>();

            List<MemberViewModel> _members = JsonConvert.DeserializeObject<List<MemberViewModel>>(Properties.Resources.Developers);
            IEnumerable<MemberViewModel> _ordereds = _members.OrderBy(member => member.English);
            foreach (MemberViewModel _member in _ordereds) Members.Add(_member);

            string[] _files = Directory.GetFiles(Directory.GetCurrentDirectory());
            foreach (string _file in _files)
            {
                if (_executable.Location != _file && ".dll" == Path.GetExtension(_file).ToLower())
                {
                    ModuleViewModel _module = new ModuleViewModel(_file);
                    bool _exist = false;
                    foreach (ModuleViewModel _contained in Modules)
                    {
                        if (_contained.Name == _module.Name)
                        {
                            _exist = true;
                            break;
                        }
                    }
                    if (false == _exist) Modules.Add(_module);
                }
            }
        }
        public static ProgramProfileViewModel GetInstance()
        {
            if (null == mInstance) mInstance = new ProgramProfileViewModel();
            return mInstance;
        }

        private void RaisePropertyChanged(string strPropertyName)
        {
            PropertyChangedEventHandler _handler = this.PropertyChanged;
            if (null != _handler) _handler(this, new PropertyChangedEventArgs(strPropertyName));
        }
    }
}